<?php
session_start();
$host = 'localhost';
$username = '';
$password = '';
$dbname = '';
$id = $_SESSION['id']; //读取读者id
$book_id = $_POST['id']; //读取借阅书号
$keep_time = $_POST['keep'];
$url1 = "Homepage.html";
$mysql = new mysqli($host, $username, $password, $dbname);
if ($mysql->connect_errno) {
    die('数据库连接失败：' . $mysql->connect_errno);
} else { //首先检测这本书的库存
    $sql1 = "SELECT num,book_name from bookinfo where book_id='$book_id'";
    $result1 = $mysql->query($sql1);
    $row1 = mysqli_fetch_row($result1);
    $sql2 = "SELECT * FROM readerinfo where reader_id=$id";
    $result2 = $mysql->query($sql2);

    $row2 = mysqli_fetch_row($result2);
    $reader_name = $row2[0]; //获得借阅者姓名，要插入b_r表
    $book_name = $row1[1];
    if ($row1[0] == 0) {
        echo "<script>alert('这本书没有啦,即将返回首页')</script>";
        echo "<script language='javascript' type='text/javascript'>";
        echo "window.location.href='$url1'"; //回到首页
        echo "</script>";
    }
    if ($row1[0] == 1) { //只有一本

        //$sql5 = "SELECT book_name from bookinfo where book_id=$book_id";
        //$result5 = $mysql->query($sql5);
        //$row5 = mysqli_fetch_row($result5); //得到书名
        $sql4 = "INSERT INTO b_r(reader_id,reader_name,book_id,book_name,keep_time) VALUES($id,'$reader_name',$book_id,'$book_name',$keep_time)";
        $result4 = $mysql->query($sql4);
        $sql8 = "UPDATE readerinfo SET `keep`=`keep`+1 where reader_id=$id";
        $result8 = $mysql->query($sql8);
        if ($result4) {
            $sql3 = "UPDATE bookinfo set num=0,on_shelf='否' where book_id=$book_id"; //更新bookinfo表
            $result3 = $mysql->query($sql3);

            if ($result3 && $result4) {
                echo "<script>alert('借阅成功')</script>";
                echo "<script language='javascript' type='text/javascript'>";
                echo "window.location.href='$url1'"; //跳转到首页
                echo "</script>";
            } else {
                echo "<script>alert('借阅失败')</script>";
                echo "<script language='javascript' type='text/javascript'>";
                echo "window.location.href='$url1'"; //跳转到首页
                echo "</script>";
            }
        } else {
            echo "<script>alert('借阅失败')</script>";
            echo "<script language='javascript' type='text/javascript'>";
            echo "window.location.href='$url1'"; //跳转到首页
            echo "</script>";
        }
    }
    if ($row1[0] > 1) {
        $sql7 = "INSERT INTO b_r(reader_id,reader_name,book_id,book_name,keep_time) VALUES($id,'$reader_name',$book_id,'$book_name',$keep_time)";
        $result7 = $mysql->query($sql7);
        $sql9 = "UPDATE readerinfo SET `keep`=`keep`+1 where reader_id=$id";
        $result8 = $mysql->query($sql9);
        if ($result7) {
            $sql6 = "UPDATE bookinfo set num=num-1 where book_id=$book_id"; //更新bookinfo表
            $result6 = $mysql->query($sql6);
            if ($result6) {
                echo "<script>alert('借阅成功')</script>";
                echo "<script language='javascript' type='text/javascript'>";
                echo "window.location.href='$url1'"; //跳转到首页
                echo "</script>";
            } else {
                echo "<script>alert('借阅失败')</script>";
                echo "<script language='javascript' type='text/javascript'>";
                echo "window.location.href='$url1'"; //跳转到首页
                echo "</script>";
            }
        } else {
            echo "<script>alert('借阅失败')</script>";
            echo "<script language='javascript' type='text/javascript'>";
            echo "window.location.href='$url1'"; //跳转到首页
            echo "</script>";
        }
    }
}
$mysql->close();
unset($_SESSION['id']);
